package com.lib.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.util.Log;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
 * @author FengLing
 * Created on 2017-05-26 18:00
 */
public class Base64Utils {
    private Base64Utils() {
        throw new NullPointerException("Why you wanna me ?");
    }

    /**
     * 将字节数组编码为字符串
     *
     * @param bytes
     */
    public static String encodeToString(byte[] bytes) {
        if (bytes == null) {
            return null;
        }
        String encoded = Base64.encodeToString(bytes, Base64.DEFAULT);
        System.out.println("Base 64 加密后：" + encoded);
        return encoded;
    }

    public static String decodeToString(String str) {
        if (str == null) {
            return null;
        }
        byte[] decoded = Base64.decode(str, Base64.DEFAULT);
        String decodeStr = new String(decoded);
        System.out.println("Base 64 解密后：" + decodeStr);
        return decodeStr;
    }

    public static String bitmapToString(Bitmap bitmap) {
        if (bitmap == null) {
            return null;
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] bytes = baos.toByteArray();

//        int byteCount = bitmap.getByteCount();
//        ByteBuffer bb = ByteBuffer.allocate(byteCount);
//        bitmap.copyPixelsToBuffer(bb);
//        byte[] bytes = bb.array();
//
        String string = Base64.encodeToString(bytes, Base64.DEFAULT);
        return string;
    }

    public static Bitmap stringToBitmap(String string) {
        Bitmap bitmap = null;
        try {
            byte[] bitmapArray = Base64.decode(string, Base64.DEFAULT);
            bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
        } catch (Throwable e) {
            if (e instanceof OutOfMemoryError) {
                XLog.e("stringToBitmap->" + Log.getStackTraceString(e));
            }
        }
        return bitmap;
    }

    /**
     * 文件转base64字符串
     *
     * @param file
     * @return
     */
    public static String fileToBase64(File file) {
        String base64 = null;
        try (InputStream in = new FileInputStream(file)) {
            byte[] bytes = new byte[in.available()];
            int length = in.read(bytes);
            byte[] encode = Base64.encode(bytes, 0, length, Base64.DEFAULT);
            base64 = new String(encode, StandardCharsets.UTF_8);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return base64;
    }

    /**
     * base64字符串转文件
     *
     * @param base64
     * @return
     */
    public static void base64ToFile(String base64, File file) {
        if (file.exists()) {
            file.delete();
        }

        FileOutputStream out = null;
        ByteArrayInputStream in = null;
        try {
            byte[] bytes = base64.getBytes(StandardCharsets.UTF_8);// 将字符串转换为byte数组
            byte[] decode = Base64.decode(bytes, Base64.DEFAULT);

            in = new ByteArrayInputStream(decode);
            out = new FileOutputStream(file);

            byte[] buffer = new byte[decode.length];
            int read = in.read(buffer);

            out.write(buffer, 0, read);
        } catch (IOException ioe) {
            ioe.printStackTrace();
            file.delete();
        } finally {
            CloseUtils.closeIOQuietly(in, out);
        }
    }
}